package ils.domain.catalog.model;

import ils.util.HibernateSession;
import ils.util.HibernateSessionManager;
import ils.util.SpringUtil;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: rejeevd
 * Date: 23/09/12
 * Time: 21:56
 * To change this template use File | Settings | File Templates.
 */
public class CategoryRepositoryTest
{
    private HibernateSessionManager hibernateSessionManager = new HibernateSessionManager();
    private ApplicationContext context = SpringUtil.getApplicationContext();
    private CategoryRepository categoryRepository = (CategoryRepository)context.getBean("categoryRepository");

    @Test
    public void testGetCategory()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Category category = categoryRepository.getCategory(6);
            Assert.assertNotNull(category);
            Assert.assertEquals(4, (int)category.getParentId());

            category = categoryRepository.getCategory(8);

            category = categoryRepository.getCategory(1);
            Assert.assertNull(category.getParentId());
        }
    }

    @Test
    public void testGetDescendants()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Category> categories = categoryRepository.getDescendants(1);
            Assert.assertEquals(2, categories.size());
        }
    }

    @Test
    public void testGetRelated()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Category> related = categoryRepository.getRelated(6);
            Assert.assertEquals(2, related.size());
        }
    }

    @Test
    public void testGetParent()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Category parent = categoryRepository.getParent(4);
            Assert.assertNotNull(parent);
            Assert.assertEquals(1, (int)parent.getId());
        }
    }

    @Test
    public void testGetChildren()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Category> children = categoryRepository.getChildren(1);
            Assert.assertEquals(1, children.size());
        }
    }
}
